package com.laolang.ssh.v1.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.laolang.ssh.domain.BookType;
import com.laolang.ssh.service.BookTypeService;
import com.laolang.ssh.util.JsonUtil;
import com.laolang.ssh.v1.pojo.BookTypeResultPojo;
import com.laolang.ssh.v1.pojo.ResultPojo;

@Controller
@RequestMapping("/v1/booktypes")
public class BookTypesController {
	
	Logger log = LoggerFactory.getLogger(BookTypesController.class);
	
	@Autowired
	private BookTypeService bookTypeService;

	@RequestMapping(method = RequestMethod.POST, produces = "text/json")
	public String BookTypeList(@RequestParam(value = "page", required = false) Integer page,
			@RequestParam(value = "size", required = false) Integer size, Model model, HttpSession session, HttpServletResponse response,
			HttpServletRequest request) throws IOException {
		log.info("发送图书类别信息");

		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		String jsonStr;
		
		
		Integer firstResult = null == page ? 1 : page;
		Integer maxResults = null == size ? 10 : size;
		List<BookType> bookTypes = bookTypeService.findAll(firstResult, maxResults);
		BookTypeResultPojo bookTypeResultPojo ;
		if( bookTypes.size() > 0 ){
			bookTypeResultPojo = new BookTypeResultPojo(ResultPojo.SUCCESS, bookTypes);
		}else{
			bookTypeResultPojo = new BookTypeResultPojo(ResultPojo.FAILED, bookTypes);
		}
		jsonStr = JsonUtil.toJson( bookTypeResultPojo );
		out.print(jsonStr);
		out.flush();
		out.close();
		return null;
	}

}
